package comp512.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.ConnectException;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import comp512.classes.Player;

@SuppressWarnings("serial")
public class ClientPrompt extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtServerAddress;
	private JTextField txtPort;
	private JTextField txtUsername;
	private JTextField txtStatus;
	private Player player = null;
	/**
	 * Create the dialog.
	 */
	public ClientPrompt() {
		setTitle("Client Connection Information");
		setBounds(100, 100, 450, 199);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e){
				if(player != null){
					player.disconnect();
				}
			}
		});
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.CENTER);
			panel.setLayout(new MigLayout("", "[][grow]", "[][][][]"));
			{
				JLabel lblServerAddress = new JLabel("Server Address");
				lblServerAddress.setBorder(new EmptyBorder(0, 5, 5, 5));
				panel.add(lblServerAddress, "cell 0 0,alignx trailing");
			}
			{
				txtServerAddress = new JTextField();
				txtServerAddress.setText("127.0.0.1");
				txtServerAddress.addFocusListener(new FocusAdapter() {
					@Override
					public void focusGained(FocusEvent arg0) {
						if (txtServerAddress.getText().equals("Address"))
							txtServerAddress.setText("");
					}

					public void focusLost(FocusEvent e) {
						if (txtServerAddress.getText().length() < 1) {
							txtServerAddress.setText("Address");
						}
					}
				});
				panel.add(txtServerAddress, "cell 1 0,growx,aligny center");
				txtServerAddress.setColumns(10);
			}
			{
				JLabel lblPort = new JLabel("Port");
				lblPort.setBorder(new EmptyBorder(5, 5, 5, 5));
				panel.add(lblPort, "cell 0 1,alignx trailing");
			}
			{
				txtPort = new JTextField();
				txtPort.setText("6060");
				txtPort.addFocusListener(new FocusAdapter() {
					@Override
					public void focusGained(FocusEvent e) {
						if (txtPort.getText().equals("Port")) {
							txtPort.setText("");
						}
					}

					public void focusLost(FocusEvent e) {
						if (txtPort.getText().length() < 1) {
							txtPort.setText("Port");
						}
					}
				});
				panel.add(txtPort, "cell 1 1,growx");
				txtPort.setColumns(10);
			}
			{
				JLabel lblUsername = new JLabel("Username");
				lblUsername.setBorder(new EmptyBorder(5, 5, 5, 5));
				panel.add(lblUsername, "cell 0 2,alignx trailing");
			}
			{
				txtUsername = new JTextField();
				txtUsername.setText("Username");
				txtUsername.addFocusListener(new FocusAdapter() {
					@Override
					public void focusGained(FocusEvent e) {
						if (txtUsername.getText().equals("Username")) {
							txtUsername.setText("");
						}
					}

					public void focusLost(FocusEvent e) {
						if (txtUsername.getText().length() < 1) {
							txtUsername.setText("Username");
						}
					}
				});
				txtUsername.setDoubleBuffered(true);
				panel.add(txtUsername, "cell 1 2,growx");
				txtUsername.setColumns(10);
			}
			{
				JLabel lblStatus = new JLabel("Status");
				lblStatus.setBorder(new EmptyBorder(5, 5, 5, 5));
				panel.add(lblStatus, "cell 0 3,alignx trailing");
			}
			{
				txtStatus = new JTextField();
				txtStatus.setText("Status");
				txtStatus.setEditable(false);
				txtStatus.setDoubleBuffered(true);
				txtStatus.setColumns(10);
				panel.add(txtStatus, "cell 1 3,growx");
			}
		}

		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		{
			JButton okButton = new JButton("OK");
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					int portVal;
					try{
						portVal = Integer.parseInt(txtPort.getText());
					}catch(NumberFormatException nfe){
						System.err.println("Invalid Port");
						return;
					}
					try {
						player = new Player(txtServerAddress.getText(), portVal, txtUsername.getText());
						try {
							Thread.sleep(10000);
						} catch (InterruptedException e) {
							// TODO 
							e.printStackTrace();
						}
					} catch (UnknownHostException e) {
						// TODO 
						e.printStackTrace();
					} catch (ConnectException ce) {
						System.err.println("Connection Error:" + ce.getMessage());
						ce.printStackTrace();
					} catch (IOException e) {
						// TODO 
						e.printStackTrace();
					}
					if(player.getSocket().isConnected())
						startLobby(player);
					else{
						//TODO
					}
				}
			});
			okButton.setActionCommand("OK");
			buttonPane.add(okButton);
			getRootPane().setDefaultButton(okButton);
		}
		{
			JButton cancelButton = new JButton("Cancel");
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					closeDialog();
				}
			});
			cancelButton.setActionCommand("Cancel");
			buttonPane.add(cancelButton);
		}

	}

	private final void closeDialog() {
		dispose();
		System.exit(0);
	}

	private final void startLobby(Player player) {
		
		ClientGUI_Lobby client = new ClientGUI_Lobby();
		
		client.setVisible(true);
		this.setVisible(false);
	}

}
